package org.zjt.book

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks._
/**
  * DESC    程序实现的单词计数
  *
  * @author
  * @create 2017-04-26 下午2:31
  **/
object ScalaWordCount {

  def main(args: Array[String]): Unit = {
    var arrayBuffer = ArrayBuffer[Tuple2[String, Int]]()
    var map = mutable.HashMap[String, Int]()
    val context = scala.io.Source.fromFile("test.txt", "utf8").mkString
    var array = context.split(",").map(_.trim)


    println("开始： "+array.mkString(","))
    breakable{
      for (i <- array) {
        array = array.filter(a => !map.contains(a))
        if (array.isEmpty) break()
        var result = array.map((_, 1)).reduceLeft((B, A) => { if (A._1 == B._1) (B._1, B._2 + 1) else B })
        arrayBuffer += result
        map += result
        printf("执行： result:%-20s \t array:%-50s\n",result,array.mkString(","))
      }
    }
    println("结果： "+map.mkString(","))


  }

}
